#define DEBUG
#include <cstdio>

using namespace std;

const int maxh=4000, maxw=4000;

int max(int a, int b) {
  return a>b ? a : b;
}

void solveBook(int x, int y, int h, int w, char map[maxh+1][maxw+1], int book[maxh+1][maxw+1]) {
  static const int fx[4][2]={{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
  for (int i=0; i<4; i++) {
    int nx=x+fx[i][0], ny=y+fx[i][1];
    if (nx>0 && ny>0 && nx<=h && ny<=w && !book[nx][ny] && map[nx][ny]==map[x][y]) {
      book[nx][ny]=book[x][y];
      solveBook(nx, ny, h, w, map, book);
    }
  }
  for (int i=0; i<4; i++) {
    int nx=x+fx[i][0], ny=y+fx[i][1];
    if (nx>0 && ny>0 && nx<=h && ny<=w && !book[nx][ny] && map[nx][ny]!=map[x][y] && map[nx][ny]!='.') {
      book[nx][ny]=book[x][y]+1;
      solveBook(nx, ny, h, w, map, book);
    }
  }
}

int main() {
#ifdef DEBUG
  freopen("1.in", "r", stdin);
  freopen("1.out", "w", stdout);
#endif

  static char map[maxh+1][maxw+1];
  static int book[maxh+1][maxw+1];
  int h, w;
  scanf("%d %d", &h, &w);
  for (int i=1; i<=h; i++) {
    for (int j=1; j<=w; j++) {
      scanf(" %c ", &map[i][j]);
    }
  }

  book[1][1]=1;
  solveBook(1, 1, h, w, map, book);
  int ans=0;
  for (int i=1; i<=h; i++) {
    for (int j=1; j<=w; j++) {
      ans = max(ans, book[i][j]);
    }
  }
  printf("%d", ans);

  fclose(stdin);
  fclose(stdout);
  return 0;
}
